package com.sevencar.activity;

import com.sevencar.R;

import me.maxwin.view.TopIndicator;
import me.maxwin.view.TopIndicator.OnTopIndicatorListener;
import me.maxwin.view.XListView.IXListViewListener;
import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class OrderManageFragment extends Fragment implements IXListViewListener ,OnTopIndicatorListener{
	
	
	private Activity mActivity;
	private TextView mTitleTv;
	private ViewPager mViewPager;
	private TabPagerAdapter mPagerAdapter;
	private TopIndicator mTopIndicator;
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		this.mActivity = activity;
	}

	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		
		
			
		
		return inflater.inflate(R.layout.fragment_order, container, false);
	}
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		
		initDisplay();
	}
	
	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		initViews(view);
	}
	private void initViews(View view) {
		mTitleTv = (TextView) view.findViewById(R.id.title_tv);
		mTitleTv.setText("商城");
		
		mViewPager = (ViewPager) view.findViewById(R.id.view_pager);
		mPagerAdapter = new TabPagerAdapter(getFragmentManager());
		
		mTopIndicator = (TopIndicator) view.findViewById(R.id.top_indicator);
		mTopIndicator.setOnTopIndicatorListener(this);
	}
	
	
	private void initDisplay() {
		mViewPager.setAdapter(mPagerAdapter);
		mViewPager.invalidate();
		mPagerAdapter.notifyDataSetChanged();
	}
	
	@Override
	public void onRefresh() {
		
	}

	@Override
	public void onLoadMore() {
		
	}
	
	
	private class TabPagerAdapter extends FragmentStatePagerAdapter implements
	ViewPager.OnPageChangeListener {
	
	public TabPagerAdapter(FragmentManager fm) {
		super(fm);
		mViewPager.setOnPageChangeListener(this);
	}
	
	@Override
	public Fragment getItem(int position) {
		HomeTabFragment fragment = (HomeTabFragment) Fragment
				.instantiate(mActivity,
						HomeTabFragment.class.getName());
		fragment.setMsgName("message name " + position);
		return fragment;
	}
	
	@Override
	public int getCount() {
		return 4;
	}
	
	@Override
	public void onPageScrollStateChanged(int arg0) {
	
	}
	
	@Override
	public void onPageScrolled(int position, float positionOffset,
			int positionOffsetPixels) {
		
	}
	
	@Override
	public void onPageSelected(int position) {
		mTopIndicator.setTabsDisplay(mActivity, position);
	}
	}
	
	@Override
	public void onIndicatorSelected(int index) {
	mViewPager.setCurrentItem(index);
	}

}
